package two;

public class A028 {


    //两数之和 -- 看代码时把下面的注释去掉

    public ListNode reverse(ListNode head) {
        ListNode newHead = new ListNode();
        ListNode cur = newHead;
        while(head != null) {
            ListNode next = head.next;
            cur.next = head;
            cur = cur.next;
            head = next;
        }
        return newHead.next;
    }

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        l1 = reverse(l1);
        l2 = reverse(l2);
        int t=0;
        ListNode head = new ListNode();
        ListNode cur = head;
        while(l1!=null || l2!=null) {
            if(l1!=null) {
                //t += l1.val;
                l1 = l1.next;
            }
            if(l2!=null) {
                //t += l2.val;
                l2 = l2.next;
            }
            //cur.next = new ListNode(t%10);
            t = t/10;
            cur = cur.next;
        }
        if(t!=0) {
            //cur.next = new ListNode(t%10);
        }
        cur = head.next;
        head=null;
        return reverse(cur);
    }
}
